package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.dto.ElderPageQueryDto;
import com.zzyl.nursing.domain.pojo.Elder;
import com.zzyl.nursing.domain.vo.ElderVo;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.service.IElderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 老人Service业务层处理
 *
 * @author alexis
 * @date 2025-05-19
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService {
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     *
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id) {
        return elderMapper.selectById(id);
    }

    /**
     * 查询老人列表
     *
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder) {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人
     *
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder) {
        return elderMapper.insert(elder);
    }

    /**
     * 修改老人
     *
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder) {
        return elderMapper.updateById(elder);
    }

    /**
     * 批量删除老人
     *
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids) {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人信息
     *
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id) {
        return elderMapper.deleteById(id);
    }

    /**
     * 查询老人列表(设备管理模块)
     */
    @Override
    public TableDataInfo<ElderVo> pageQuery(ElderPageQueryDto elderPageQueryDto) {
        //分页查询
        Page<Elder> page = Page.of(elderPageQueryDto.getPageNum(), elderPageQueryDto.getPageSize());
        String name = elderPageQueryDto.getName();
        String idCardNo = elderPageQueryDto.getIdCardNo();
        this.page(page, Wrappers.lambdaQuery(Elder.class)
                .like(StrUtil.isNotBlank(name), Elder::getName, name)
                .like(StrUtil.isNotBlank(idCardNo), Elder::getIdCardNo, idCardNo)
                .eq(Elder::getStatus, 1)
                .orderByDesc(Elder::getCreateTime));
        //返回Vo对象(补充TableDataInfo中的有参构造器,用于匹配当前的响应参数形式)
        List<Elder> elderList = page.getRecords();
        List<ElderVo> elderVoList = BeanUtil.copyToList(elderList, ElderVo.class);
        return TableDataInfo.getDataTable(elderVoList, page.getTotal());

    }
}
